<?php
namespace Home\Controller;
use Think\Controller;

class SendwxcodeController extends  BaseController
{
	public function index()
	{
		$mobile = addslashes(htmlspecialchars(trim($GLOBALS['request']['mobile'])));
	
		$root = array();
		
		if(app_conf("SMS_ON")==0)
		{
			$root['response_code'] = 0;
			$root['show_err'] = $GLOBALS['lang']['SMS_OFF'];//短信未开启
			output($root);
		}
	
	
		if($mobile == '')
		{
			$root['response_code'] = 0;
			$root['show_err'] = $GLOBALS['lang']['MOBILE_EMPTY_TIP'];//请输入你的手机号
			output($root);
		}
	
		if(!check_mobile($mobile))
		{
			$root['response_code'] = 0;
			$root['show_err'] = $GLOBALS['lang']['FILL_CORRECT_MOBILE_PHONE'];//请填写正确的手机号码
			output($root);
		}
	
		//if($GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."user where mobile = '".$mobile."'")>0)
		if($GLOBALS['db']->getOne("select count(*) from ".DB_PREFIX."user where user_type>1 AND mobile_encrypt = AES_ENCRYPT('".$mobile."','".AES_DECRYPT_KEY."') ")>0)
		{ 
			$field_show_name = $GLOBALS['lang']['USER_TITLE_mobile'];//手机号码
			$root['response_code'] = 0;
			$root['show_err'] = "该手机号码无法使用";//sprintf($GLOBALS['lang']['EXIST_ERROR_TIP'],$field_show_name); //已存在，请重新输入
			output($root);
		}
		
		if(!check_ipop_limit(CLIENT_IP,"mobile_verify",60,0))
		{
			$root['response_code'] = 0;
			$root['show_err'] = $GLOBALS['lang']['MOBILE_SMS_SEND_FAST']; //短信发送太快
			output($root);
		}
	
		$verify_data['verify_code'] = rand(111111,999999);
		$verify_data['mobile'] = $mobile;
		$verify_data['create_time'] = TIME_UTC;
		$verify_data['client_ip'] = CLIENT_IP;
	
		if($vid = $GLOBALS['db']->getOne("SELECT id FROM ".DB_PREFIX."mobile_verify_code WHERE mobile='".$mobile."'"))
			$GLOBALS['db']->autoExecute(DB_PREFIX."mobile_verify_code",$verify_data,"UPDATE","id=".$vid);
		else
			$GLOBALS['db']->autoExecute(DB_PREFIX."mobile_verify_code",$verify_data,"INSERT");
		
		$result = send_verify_sms($mobile,$verify_data['verify_code'],null,true);//
		
		//$root['response_code'] = $result['status'];
		$root['response_code'] = 1;
		
		if ($root['response_code'] == 1){
			$root['show_err'] = $GLOBALS['lang']['MOBILE_VERIFY_SEND_OK'];
		}else{
			$root['show_err'] = $result['msg'];
			if ($root['show_err'] == null || $root['show_err'] == ''){
				$root['show_err'] = "验证码发送失败";
			}
		}
		//../system/sms/FW_sms.php  提示账户或密码错误地址
		
		output($root);
	}
	
}
?>